package com.mine.modules.vm.common.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * @Author xu
 * @Date 2025/4/7
 **/
public class CostConverter implements Converter<BigDecimal> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return BigDecimal.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    /**
     * 这里读的时候会调用
     */
    @Override
    public BigDecimal convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty property, GlobalConfiguration config) {
        System.out.println("cellData.getRowIndex()==1111" + cellData.getRowIndex());
        System.out.println("cellData.getType()==1111" + cellData.getType());
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            return new BigDecimal(0); // 返回 null 或默认值（如 0）
        }
        if (cellData.getType() == CellDataTypeEnum.NUMBER) {
            BigDecimal numValue = cellData.getNumberValue();
            if (numValue == null || numValue.compareTo(BigDecimal.ZERO) == 0) {
                return new BigDecimal(0);
            }
            return numValue;
        }
        if (cellData.getType() == CellDataTypeEnum.STRING) {
            String value = cellData.getStringValue();
            if (StringUtils.isNotEmpty(value) && !StringUtils.equals("null", value) && !StringUtils.equals("无", value)) {
                return BigDecimal.valueOf(Double.parseDouble(value));
            } else {
                return new BigDecimal(0);
            }
        }
        return new BigDecimal(0);
    }

    /**
     * 这里是写的时候会调用 不用管
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<BigDecimal> context) {
        return new WriteCellData<>(String.valueOf(context.getValue()));
    }

}
